package com.owen.controller;

import com.owen.service.UserService;
import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index(){
        return "index";
    }

    // 导出数据
    @RequestMapping(value = "/down", method = RequestMethod.POST)
    public void down(HttpServletResponse response) throws IOException, WriteException, IllegalAccessException {
        userService.downLoadExcel(response);
    }

    @RequestMapping(value = "/img", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, String> img() throws IOException {
        // 读取svg文件
        InputStream in = getClass().getResourceAsStream("/private/user.svg");
        // 将字节流对象转成字符流对象,使用转换流 InputStreamReader()
        InputStreamReader isr = new InputStreamReader(in);//传入 键盘录入对象 in
        // 为了提高效率,将字符串进行缓冲区技术高效操作.使用BufferedReader
        BufferedReader bufr = new BufferedReader(isr);
        // 用户存放读取出来的字符串
        StringBuffer stringBuffer = new StringBuffer();
        // 占存字符串
        String line = null;
        // 在BufferedReader中逐行读取
        while ((line = bufr.readLine()) != null) {   // 判断 读取行 非空
            // 读取出来的某行字符串拼接到StringBuffer并换行
            stringBuffer.append(line).append(System.getProperty("line.separator", "\n"));
        }
        // 关闭BufferedReader
        bufr.close();
        // 关闭InputStreamReader
        isr.close();
        // 关闭InputStream
        in.close();
        Map<String,String> map = new HashMap<String,String>();
        map.put("status","200");
        map.put("data",stringBuffer.toString());
        map.put("error","");
        // 返回svg内容
        return map;
    }

}
